891A - Pride - CodeForces Solution


brute force dp greedy math number theory *1500

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#define ll long long
using namespace std;

void solve() {
    int n,i,j,ans=4004,ones=0;
    cin >> n;
    vector<int> a(n);
    for(i=0;i<n;i++) {
        cin >> a[i];
        if(a[i]==1) ones++;
    }
    if(ones==n) {
        cout << 0 << endl;
        return;
    }
    for(i=0;i<n-1;i++) {
        int check=a[i+1];
        for(j=i+1;j<n;j++) {
            check=gcd(check,a[j]);
            if(gcd(a[i],check)==1) ans=min(ans,j-i+n-1);
        }
    }
    if(ans==4004) cout << -1 << endl;
    else cout << ans-ones << endl;
}

int main() {
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    int t=1;
    // cin >> t;
    while(t--) {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament